<?php

/*
  $Id: address_book.php,v 1.58 2003/06/09 23:03:52 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

require('includes/application_top.php');

if (!$account->isRegistered())
{
	$navigation->setSnapshot();
	tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
}

$theme->config_load('address_book.conf');

$template = 'address_book.tpl';
$addressBook = new AddressBook($customer_id);

if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'deleteconfirm') && isset($HTTP_POST_VARS['delete'])) {
	$addressBook->deleteEntry((int) $HTTP_POST_VARS['delete']);
	$messageStack->addSession('addressbook', $theme->get_config_vars('SUCCESS_ADDRESS_BOOK_ENTRY_DELETED'), 'success');
	tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
}

// error checking when updating or adding an entry
$process = false;

if (isset($HTTP_POST_VARS['action']) && (($HTTP_POST_VARS['action'] == 'process') || ($HTTP_POST_VARS['action'] == 'update'))) {
	if (isset($HTTP_POST_VARS['edit']) && is_numeric($HTTP_POST_VARS['edit']))
	{
		$contact = new Contact($customer_id, $HTTP_POST_VARS['edit']);
		
		if (!$contact->exists())
		{
			$messageStack->addSession('addressbook', $theme->get_config_vars('ERROR_NONEXISTING_ADDRESS_BOOK_ENTRY'));
			tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
		}
	}
	
	else
	{
		$contact = new Contact($customer_id);
	}
	
	$process = true;
	$error = false;
	
	if (ACCOUNT_GENDER == 'true')
	{
		if (!$contact->setGender($HTTP_POST_VARS['gender']))
		{
			$error = true;
			$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_GENDER_ERROR'));
		}
	}
	
	if (!$contact->setFirstName($HTTP_POST_VARS['firstname']))
	{
		$error = true;
		$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_FIRST_NAME_ERROR'));
	}
	
	if (!$contact->setLastName($HTTP_POST_VARS['lastname']))
	{
		$error = true;
		$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_LAST_NAME_ERROR'));
	}
	
	if (ACCOUNT_COMPANY == 'true')
	{
		if (!$contact->setCompany($HTTP_POST_VARS['company']))
		{
			$error = true;
			$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_COMPANY_ERROR'));
		}
	}
	
	if (!$contact->setStreet($HTTP_POST_VARS['street_address']))
	{
		$error = true;
		$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_STREET_ADDRESS_ERROR'));
	}
	
	if (ACCOUNT_SUBURB == 'true')
	{
		if (!$contact->setSuburb($HTTP_POST_VARS['suburb']))
		{
			$error = true;
			$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_SUBURB_ERROR'));
		}
	}
	
	if (!$contact->setPostcode($HTTP_POST_VARS['postcode']))
	{
		$error = true;
		$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_POST_CODE_ERROR'));
	}
	
	if (!$contact->setCity($HTTP_POST_VARS['city']))
	{
		$error = true;
		$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_CITY_ERROR'));
	}
	
	if (!$contact->setCountry($HTTP_POST_VARS['country']))
	{
		$error = true;
		$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_COUNTRY_ERROR'));
	}
	
	if (ACCOUNT_STATE == 'true')
	{
		if (!$contact->setState($state))
		{
			$error = true;
			$messageStack->add('addressbook', $theme->get_config_vars('ENTRY_STATE_ERROR'));
		}
	}
	
	if (!$error)
	{
		// TODO: Some error checking probably needed here?!
		$contact->saveChanges();
		
		if ((isset($HTTP_POST_VARS['primary']) && ($HTTP_POST_VARS['primary'] == 'on')) || ($HTTP_POST_VARS['edit'] == $addressBook->getPrimaryAddress()) || ($addressBook->getPrimaryAddress() < 1))
		{
			$addressBook->setPrimaryAddress($contact);
		}
		
		$messageStack->addSession('addressbook', $theme->get_config_vars('SUCCESS_ADDRESS_BOOK_ENTRY_UPDATED'), 'success');
		tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));	
	}
	
	$template = 'address_book_edit.tpl';
}

if (isset($HTTP_GET_VARS['add']))
{
	if (tep_count_customer_address_book_entries() >= MAX_ADDRESS_BOOK_ENTRIES)
	{
		$messageStack->addSession('addressbook', $theme->get_config_vars('ERROR_ADDRESS_BOOK_FULL'));
		tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
	}
	
	$contact = new Contact($customer_id);
	$template = 'address_book_edit.tpl';
}

elseif (isset($HTTP_GET_VARS['edit']) && is_numeric($HTTP_GET_VARS['edit']))
{
	$contact = $addressBook->getAddressById((int) $HTTP_GET_VARS['edit']);
	
	if (!$contact->exists())
	{
		$messageStack->addSession('addressbook', $theme->get_config_vars('ERROR_NONEXISTING_ADDRESS_BOOK_ENTRY'));
		tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
	}
	
	$template = 'address_book_edit.tpl';
}

elseif (isset($HTTP_GET_VARS['delete']) && is_numeric($HTTP_GET_VARS['delete']))
{
	if ($HTTP_GET_VARS['delete'] == $customer_default_address_id)
	{
		$messageStack->addSession('addressbook', $theme->get_config_vars('WARNING_PRIMARY_ADDRESS_DELETION'), 'warning');
		tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
	}
	
	else
	{
		$contact = $addressBook->getAddressById((int) $HTTP_GET_VARS['delete']);
		
		if (!$contact->exists())
		{
			$messageStack->addSession('addressbook', $theme->get_config_vars('ERROR_NONEXISTING_ADDRESS_BOOK_ENTRY'));
			tep_redirect(tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL'));
		}
		
		// TODO: I'm sure there's a better way to do this which is less hardcoded
		$template = 'address_book_delete.tpl';
	}
}

else
{
	$entry = array();
}

$theme->register_function('getZones', 'getZones');

$theme->assign_by_ref('address_book', $addressBook);

if (isset($contact))
{
	$theme->assign_by_ref('contact', $contact);
}

$theme->assign('countries', tep_get_countries());
$theme->display($template);

require(DIR_WS_INCLUDES . 'application_bottom.php');

// TODO: This probably needs a better home!

function getZones()
{
	$zones = array();
	$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
	
	while ($zones_values = tep_db_fetch_array($zones_query))
	{
		$zones[] = array(
			'id' => $zones_values['zone_name'],
			'text' => $zones_values['zone_name']
		);
	}
	
	return $zones;
}

?>
